package impl

import (
	"github.com/jonasngs/go_entry_task/protobuf3/pb"
	"github.com/jonasngs/go_entry_task/tcp_server/storage"
	"log"
)

type UserDaoServiceImpl struct {
	db storage.Database
}

func (ds UserDaoServiceImpl) FetchUser(username string) (*pb.User, error) {

	query := "SELECT user_id, username, password, nickname, profile_picture FROM t_user WHERE username = ? limit 1"
	log.Printf("%s",*ds.db)
	
	row := ds.db.DB.QueryRow(query, username)
	var userId uint64
	var password string
	var nickname string
	var profilePicture string

	err := row.Scan(&userId, &username, &password, &nickname, &profilePicture)
	if err != nil {
		log.Printf("Error %s when Fetching user\n", err)
		return nil, err
	}

	return &pb.User{
		UserId:         userId,
		Username:       username,
		Password:       password,
		Nickname:       nickname,
		ProfilePicture: profilePicture,
	}, nil
}

func (ds UserDaoServiceImpl) UpdateNickName(username string, nickname string) error {
	query := "UPDATE t_user SET nickname=? WHERE username=?"
	_, err := ds.db.DB.Exec(query, nickname, username)
	if err != nil {
		log.Printf("Error %s when updating nickname\n", err)
	}
	return err
}

func (ds UserDaoServiceImpl) UpdateProfilePicture(username string, profile_picture string) error {
	query := "UPDATE t_user SET profile_picture=? WHERE username=?"
	_, err := ds.db.DB.Exec(query, profile_picture, username)
	if err != nil {
		log.Printf("Error %s when updating nickname\n", err)
	}
	return err
}
